package com.koron.order.grade.service.impl;

import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import com.koron.order.grade.bean.convertor.GradeConvertor;
import com.koron.order.grade.bean.entity.GradeFormBean;
import com.koron.order.grade.bean.entity.OrderGradeBean;
import com.koron.order.grade.bean.query.OrderGradeQuery;
import com.koron.order.grade.bean.vo.GradeFormVO;
import com.koron.order.grade.bean.vo.OrderGradeVO;
import com.koron.order.grade.mapper.OrderGradeMapper;
import com.koron.order.grade.service.GradeFormService;
import com.koron.order.grade.service.OrderGradeService;
import com.koron.order.overhaul.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author twcao
 * @title: OrderGradeServiceImpl
 * @projectName GDH_EAM_V2
 * @description: 分级分项验收单实现
 * @date 2021/10/1114:16
 */
@Service
public class OrderGradeServiceImpl implements OrderGradeService {

    @Autowired
    private GradeFormService gradeFormService;

    @Autowired
    private GradeConvertor convertor;

    /**
     * 通过ID查询单条数据
     *
     * @param factory 会话
     * @param id      主键
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("queryById")
    public OrderGradeVO queryById(SessionFactory factory, String id) {
        OrderGradeMapper mapper = factory.getMapper(OrderGradeMapper.class);
        OrderGradeVO gradeBean = mapper.queryById(id);
        Assert.notNull(gradeBean, "分级分项验收单不存在");
        GradeFormVO query = new GradeFormVO();
        query.setGradeId(id);
        gradeBean.setForms(gradeFormService.queryList(factory, query));
        return gradeBean;
    }

    /**
     * 根据工单id查询所有的验收标准
     *
     * @param factory
     * @param orderId
     * @return
     */
    @Override
    @TaskAnnotation("getGradeByOrderId")
    public List<OrderGradeBean> getGradeByOrderId(SessionFactory factory, String orderId) {
        OrderGradeMapper mapper = factory.getMapper(OrderGradeMapper.class);
        return mapper.getGradeByOrderId(orderId);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param factory 会话
     * @param query   实例对象
     * @return 对象列表
     */
    @Override
    @TaskAnnotation("queryList")
    public List<OrderGradeVO> queryList(SessionFactory factory, OrderGradeQuery query) {
        OrderGradeMapper mapper = factory.getMapper(OrderGradeMapper.class);
        return mapper.queryList(query);
    }

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param factory 会话
     * @param grades  实例对象
     * @return 影响行数
     */
    @Override
    public List<OrderGradeBean> saveOrUpdate(SessionFactory factory, List<OrderGradeBean> grades) {
        Assert.notEmpty(grades, "分级分项验收单集合不能为空");
        Assert.noNullElements(grades, "分级分项验收单不能为空");
        CommonUtils.update(grades);
        grades.stream().forEach(grade -> grade.setId(StringUtils.isBlank(grade.getId()) ? CodeTools.getCode32() : grade.getId()));
        OrderGradeMapper mapper = factory.getMapper(OrderGradeMapper.class);
        mapper.saveOrUpdate(grades);
        return grades;
    }

    /**
     * 批量新增或按主键更新数据（MyBatis原生foreach方法）
     *
     * @param factory 会话
     * @param vo      实例对象
     * @return
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public OrderGradeVO saveOrUpdate(SessionFactory factory, OrderGradeVO vo) {
        Assert.notNull(vo, "分级分项验收单集合不能为空");
        OrderGradeBean bean = convertor.voToBean(vo);
        CommonUtils.update(vo, ThreadLocalContext.get());
        CommonUtils.update(vo);
        OrderGradeMapper mapper = factory.getMapper(OrderGradeMapper.class);
        mapper.saveOrUpdate(Arrays.asList(bean));
        List<GradeFormVO> formVOs = vo.getForms();
        if (!CollectionUtils.isEmpty(formVOs)) {
            List<GradeFormBean> forms = new ArrayList<>();
            formVOs.stream().forEach(row -> forms.add(new GradeFormBean(row.getGradeId(), row.getFormId())));
            gradeFormService.saveOrUpdate(factory, forms);
        }
        return vo;
    }


    /**
     * 通过主键删除数据
     *
     * @param factory 会话
     * @param id      主键
     * @return 影响行数
     */
    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        OrderGradeMapper mapper = factory.getMapper(OrderGradeMapper.class);
        int i = mapper.deleteById(id);
        //删除分级分项验收单关联大修作业表单 记录
        gradeFormService.deleteByGradeId(factory, id);
        return i;
    }

    /**
     * 通过主键更新数据排序
     *
     * @param factory 会话
     * @param id      主键
     * @return 影响行数
     */
    @Override
    @TaskAnnotation("updateSortById")
    public int updateSortById(SessionFactory factory, List<OrderGradeBean> grades) {
        OrderGradeMapper mapper = factory.getMapper(OrderGradeMapper.class);

        if (CollectionUtils.isEmpty(grades)) {
            return 0;
        }
        for (int i = 0; i < grades.size(); i++) {
            grades.get(i).setSort(i);
        }
        return mapper.updateSortById(grades);
    }
}
